<?php
namespace Company\Controller;
use Think\Controller;
class RoleController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        
		
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
		$this->product = M('role',C('DB_PREFIX_T'), C('db_config'));
    }
	
	//仓配比例
	public function storage_config(){
		$m = M('DistributionPeople');
        if(I("get.sort")!=''){
            $order="m.".I("get.sort")." ".I("get.dec");
        }else{
            $order='m.id desc';
        }
        $list = $m->alias('m')->field('m.*,n.name as d_name')
            ->join("__DISTRIBUTION_DEPARTMENT__ n on n.id=m.department_id",'left')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
	}
	public function storage_price(){
		$m = M('DistributionPeople');
        if(I("get.sort")!=''){
            $order="m.".I("get.sort")." ".I("get.dec");
        }else{
            $order='m.id desc';
        }
        $list = $m->alias('m')->field('m.*,n.name as d_name')
            ->join("__DISTRIBUTION_DEPARTMENT__ n on n.id=m.department_id",'left')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
	}
    public function admin(){
        if(I("get.sort")!=''){
            $order=C('DB_PREFIX_T')."role.".I("get.sort")." ".I("get.dec");
        }else{
            $order=C('DB_PREFIX_T').'role.id desc';
        }
        $list = $this->product->field(C('DB_PREFIX_T').'role.*,'.C('DB_PREFIX_T').'department.name as d_name')
            ->join(C('DB_PREFIX_T')."department on ".C('DB_PREFIX_T')."department.id=".C('DB_PREFIX_T')."role.department_id",'left')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
    }
	//分销商列表
	public function distribution_people(){
		$m = M('DistributionPeople');
        if(I("get.sort")!=''){
            $order="m.".I("get.sort")." ".I("get.dec");
        }else{
            $order='m.id desc';
        }
        $list = $m->alias('m')->field('m.*,n.name as d_name')
            ->join("__DISTRIBUTION_DEPARTMENT__ n on n.id=m.department_id",'left')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function admin_add(){
        $this->display();
    }
	public function distribution_people_add(){
        $this->display();
    }
	
    public function admin_addD($name,$username,$nick_name,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名不能为空');
        }
		if($nick_name==''){
            alert(0,'昵称不能为空');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'角色不能为空');
        }
        $count=$this->product->where("username='".$username."'")->count();
		// echo $this->product->getlastsql();
        if($count>0){
            alert(0,'用户名已经存在');
        }
		$count=$this->product->where("nick_name='".$nick_name."'")->count();
		if($count>0){
            alert(0,'昵称已经存在');
        }
		if(!mobile($username)){
			alert(0,'用户名必须为手机号');
		}
		if(mobile($nick_name)){
			alert(0,'昵称不能为手机号');
		}
        $data['name']=$name;
		$data['nick_name']=$nick_name;
        $data['username']=$username;
        $data['salt']=rand(111111,999999);
        $data['password']=md5($password.$data['salt']);
        $data['department_id']=$department_id;
		$data['status']=1;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->add($data);
		// echo $this->product->getlastsql();
		// die;
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
	public function distribution_people_addD($name,$username,$password,$repassword,$department_id,$erp_code){
        if($name==''){
            alert(0,'姓名');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
		if($erp_code==''){
            alert(0,'erp_code不能为空');
        }
		$m = M('DistributionPeople');
        $count=$m->where("username='".$username."'")->count();
        if($count>0){
            alert(0,'用户名已经存在');
        }
        $data['name']=$name;
        $data['username']=$username;
        $data['salt']=rand(111111,999999);
        $data['password']=md5($password.$data['salt']);
        $data['department_id']=$department_id;
		$data['erp_code']=$erp_code;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$m->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function admin_edit($id){
        if($id==''){
            echo "错误";
        }
        $this->data=$this->product->find($id);
        $this->display();
    }
	public function distribution_people_edit($id){
		$m = M('DistributionPeople');
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
        $this->display();
    }
	public function storage_config_edit($id){
		$m = M('DistributionPeople');
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
        $this->display();
    }
	public function storage_price_edit($id){
		$m = M('DistributionPeople');
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
        $this->display();
    }
    public function admin_editD($id,$name,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        if($password!=''){
            $data['salt']=rand(111111,999999);
            $data['password']=md5($password.$data['salt']);
        }
        $data['name']=$name;
        $data['department_id']=$department_id;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function storage_config_editD($id,$storage_proportion,$storage_department_status){
		$m = M('DistributionPeople');
        if($storage_proportion==''){
            alert(0,'仓配比例不能为空');
        }
        $data['storage_proportion']=$storage_proportion;
		$data['storage_department_status']=$storage_department_status;
        $result=$m->where("id='".$id."'")->save($data);
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function storage_price_editD($id,$price_proportion,$price_department_status){
		$m = M('DistributionPeople');
        if($price_proportion==''){
            alert(0,'仓配比例不能为空');
        }
        $data['price_proportion']=$price_proportion;
		$data['price_department_status']=$price_department_status;
        $result=$m->where("id='".$id."'")->save($data);
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function distribution_people_editD($id,$name,$password,$repassword,$department_id){
		$m = M('DistributionPeople');
        if($name==''){
            alert(0,'姓名');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        if($password!=''){
            $data['salt']=rand(111111,999999);
            $data['password']=md5($password.$data['salt']);
        }
        $data['name']=$name;
        $data['department_id']=$department_id;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$m->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function admin_del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
	public function distribution_people_del($id){
		$m = M('DistributionPeople');
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$m->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function department(){
		$m = M('Department',C('DB_PREFIX_T'), C('db_config'));
        $f=C('DB_PREFIX_T');
        if(I("get.sort")=='name' || I("get.sort")=='id' || I("get.sort")=='operator'){
            $order=$f.'department.'.I("get.sort")." ".I("get.dec");
        }elseif(I("get.sort")=='count') {
            $order=I("get.sort")." ".I("get.dec");
        } else
        {
            $order=$f.'department.id desc';
        }
        $list = $m->field($f."department.*,count(".$f."role.id) as count")->join($f."role on ".$f."role.department_id=".$f."department.id",'left')
            ->group($f."department.id")->order($order)->select();
        $this->assign('list',$list);
        $this->display();
    }
	public function distribution_department(){
        $m = M('DistributionDepartment');
        $f=C('DB_PREFIX_T');
        if(I("get.sort")=='name' || I("get.sort")=='id' || I("get.sort")=='operator'){
            $order='m.'.I("get.sort")." ".I("get.dec");
        }elseif(I("get.sort")=='count') {
            $order=I("get.sort")." ".I("get.dec");
        } else
        {
            $order='m.id desc';
        }
        $list = $m->alias('m')->field("m.*,count(r.id) as count")->join("__DISTRIBUTION_PEOPLE__ r on r.department_id=m.id",'left')
            ->group("m.id")->order($order)->select();
        $this->assign('list',$list);
        $this->display();
    }
	//仓配比例-分销会员等级
	public function storage_config_department(){
        $m = M('DistributionDepartment');
        $f=C('DB_PREFIX_T');
        if(I("get.sort")=='name' || I("get.sort")=='id' || I("get.sort")=='operator'){
            $order='m.'.I("get.sort")." ".I("get.dec");
        }elseif(I("get.sort")=='count') {
            $order=I("get.sort")." ".I("get.dec");
        } else
        {
            $order='m.id desc';
        }
        $list = $m->alias('m')->field("m.*,count(r.id) as count")->join("__DISTRIBUTION_PEOPLE__ r on r.department_id=m.id",'left')
            ->group("m.id")->order($order)->select();
        $this->assign('list',$list);
        $this->display();
    }
	//价格比例-分销会员等级
	public function storage_price_department(){
        $m = M('DistributionDepartment');
        $f=C('DB_PREFIX_T');
        if(I("get.sort")=='name' || I("get.sort")=='id' || I("get.sort")=='operator'){
            $order='m.'.I("get.sort")." ".I("get.dec");
        }elseif(I("get.sort")=='count') {
            $order=I("get.sort")." ".I("get.dec");
        } else
        {
            $order='m.id desc';
        }
        $list = $m->alias('m')->field("m.*,count(r.id) as count")->join("__DISTRIBUTION_PEOPLE__ r on r.department_id=m.id",'left')
            ->group("m.id")->order($order)->select();
        $this->assign('list',$list);
        $this->display();
    }
	//会员等级价格详细
	public function storage_price_department_detail(){
		$distribution_department_id=trim(I('id'));
		$m=M('Product',C('DB_PREFIX_T'), C('db_config'));
		$s=M('StorageConfig',C('DB_PREFIX_T'), C('db_config'));
		$map_m['examined']=2;
		$m_list=$m->field('goods_no,name goods_name')->where($map_m)->select();
		if(empty($m_list)){
			$this->error("没有找到账册商品！");
		}
		$map_s['distribution_department_id']=$distribution_department_id;
		$s_list=$s->field('goods_no,goods_name')->where($map_s)->select();
		// dump($m_list);
		// dump($s_list);
		// die;
		foreach($m_list as $k=>$v){
			if(in_array($v,$s_list)){
				continue;
			}else{
				$arr['goods_no']=$v['goods_no'];
				$arr['goods_name']=$v['goods_name'];
				$arr['add_time']=time();
				$arr['distribution_department_id']=$distribution_department_id;
				$res=$s->add($arr);
				if(!$res){
					$this->error("同步账册商品失败！");
				}
			}
		}
		if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
		if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
		$list=$s->where($map_s)->page($p.','.$page_count)->select();
		$count=$s->where($map_s)->count();
		
		$storage_config=trim(I('storage_config'));
		
		if(!empty($storage_config)){
			// $api_url="http://es.jm.com/index.php?s=/Goodsapi/get_goodsinfo";//测试接口
			$api_url="http://www.t-cang.com/wmstest/index.php?s=/Goodsapi/get_goodsinfo";
			if(empty($api_url)){
				$this->error('请配置单个商品信息接口');
			}
			// $push_data['warehouse_code']="12345678";//测试仓库
			$push_data['warehouse_code']="11330213";
			
			foreach($list as $ks=>$vs){
				$push_data['goods_no']=$vs['goods_no'];
				$res_api=push($api_url,$push_data);
				// $res_api='{"status":1,"message":"\u67e5\u8be2\u6210\u529f\uff01","data":[{"storage_amount":"347"}]}';
				$res_api=json_decode($res_api,true);
				// dump($res_api);
				// die;
				if(empty($res_api['status'])){
					$list[$ks]['storage_amount_config']='没有查到此商品';
				}else{
					$list[$ks]['storage_amount']=$res_api['data'][0]['storage_amount'];//总库存
					$list[$ks]['storage_amount_config']=$res_api['data'][0]['storage_amount']-$vs['storage_alert_amount_config'];//剩余库存
				}
			}
		}
		
		$Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
		
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('page_count',$page_count);
		
		if(empty($storage_config)){
			$this->display();
		}else{
			$this->display('storage_config_department_detail');
		}
		
	}
	
    public function department_add(){
		$g=session('company');
        $db_name=$g['db'];
		$root=M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->order("sort")->select();
        $new_arr=array();
		foreach($root as $k=>$v){
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
	public function distribution_department_add(){
		$g=session('company');
        $db_name=$g['db'];
		$root=M('DistributionRoot')->order("sort")->select();
        $new_arr=array();
		foreach($root as $k=>$v){
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
    public function department_addD($name,$group_id){
        $g=session('company');
        $db_name=$g['db'];
        $root = M('root',C('DB_PREFIX_T'), C('db_config'));
        if($name==''){
            alert(0,'部门名称为空');
        }
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=$root->where("name='".$name."'")->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$root->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['name']=$name;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $m = M('Department',C('DB_PREFIX_T'), C('db_config'));
        $data['root_group_id']=$group_id;
        $result=$m->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
	public function distribution_department_addD($name,$group_id){
        $g=session('company');
        $db_name=$g['db'];
        $root = M('DistributionRoot');
        if($name==''){
            alert(0,'部门名称为空');
        }
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=$root->where("name='".$name."'")->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$root->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['name']=$name;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $m = M('DistributionDepartment');
        $data['root_group_id']=$group_id;
        $result=$m->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function department_edit($id){
		$m = M('Department',C('DB_PREFIX_T'), C('db_config'));
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
		// dump($this->data);
		$g=session('company');
        $db_name=$g['db'];
		$root=M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->order("sort")->select();
        $root_id=$m->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $new_arr=array();
		foreach($root as $k=>$v){
			if(in_array($v['id'],$arr_root)){
				$v['checked']=1;
			}
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if(in_array($u['id'],$arr_root)){
						$u['checked']=1;
					}
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if(in_array($us['id'],$arr_root)){
								$us['checked']=1;
							}
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if(in_array($uss['id'],$arr_root)){
										$uss['checked']=1;
									}
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
	public function distribution_department_edit($id){
		$m = M('DistributionDepartment');
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
		// dump($this->data);
		$g=session('company');
        $db_name=$g['db'];
		$root=M('DistributionRoot')->order("sort")->select();
        $root_id=$m->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $new_arr=array();
		foreach($root as $k=>$v){
			if(in_array($v['id'],$arr_root)){
				$v['checked']=1;
			}
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if(in_array($u['id'],$arr_root)){
						$u['checked']=1;
					}
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if(in_array($us['id'],$arr_root)){
								$us['checked']=1;
							}
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if(in_array($uss['id'],$arr_root)){
										$uss['checked']=1;
									}
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
	public function storage_config_department_edit($id){
		$m = M('DistributionDepartment');
        if($id==''){
            echo "错误";
        }
		$data=$m->find($id);
		$this->assign('data',$data);
        $this->display();
    }

    public function department_editD($id,$group_id){
        $g=session('company');
        $db_name=$g['db'];
        $root = M('root',C('DB_PREFIX_T'), C('db_config'));
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
		// dump($arr_id);
		// die;
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$root->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $massage=session('company');
        $data['operator']=$massage['username'];
		
		$m = M('Department',C('DB_PREFIX_T'), C('db_config'));
		
        $data['root_group_id']=$group_id;
        $result=$m->where(array('id'=>$id))->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	// public function storage_config_department_editD($id){
		// $m = M('DistributionDepartment');
		// $storage_proportion=trim(I('storage_proportion'));
        // $data['storage_proportion']=$storage_proportion;
        // $result=$m->where(array('id'=>$id))->save($data);
        // if($result!==false){
            // alert(1,'修改成功');
        // }else{
            // alert(0,'修改失败');
        // }
    // }
	public function storage_config_department_editD(){
		$m=M('StorageConfig',C('DB_PREFIX_T'), C('db_config'));
		$id=trim(I('id'));
		$storage_config=trim(I('count'));
        $data['storage_alert_amount_config']=$storage_config;
        $result=$m->where(array('id'=>$id))->save($data);
		
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function storage_price_department_editD(){
		$m=M('StorageConfig',C('DB_PREFIX_T'), C('db_config'));
		$id=trim(I('id'));
		$price_config=trim(I('count'));
        $data['price_config']=$price_config;
        $result=$m->where(array('id'=>$id))->save($data);
		
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function distribution_department_editD($id,$group_id){
        $g=session('company');
        $db_name=$g['db'];
        $root = M('DistributionRoot');
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
		// dump($arr_id);
		// die;
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$root->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $massage=session('company');
        $data['operator']=$massage['username'];
		
		$m = M('DistributionDepartment');
		
        $data['root_group_id']=$group_id;
        $result=$m->where(array('id'=>$id))->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function department_del($id){
		$m = M('Department',C('DB_PREFIX_T'), C('db_config'));
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $g=session('company');
        $db_name=$g['db'];
        $role = M('Role',C('DB_PREFIX_T'), C('db_config'));
        $count=$role->where(array('department_id'=>$id))->count();
        if($count>0){
            alert(0,'该组存在管理员，不允许删除');
        }
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$m->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
	public function distribution_department_del($id){
		$m = M('DistributionDepartment');
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $g=session('company');
        $db_name=$g['db'];
        $role = M('DistributionPeople');
        $count=$role->where(array('department_id'=>$id))->count();
        if($count>0){
            alert(0,'该组存在管理员，不允许删除');
        }
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$m->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function getRole(){
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->product->select();
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                $top[]=$v;
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    $top[$k]['child'][]=$u;
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
    public function getEditRole($id){
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->product->select();
        $this->product = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $root_id=$this->product->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                if(in_array($v['id'],$arr_root)){
                    $result[$k]['checked']=1;
                }
                $top[]=$result[$k];
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    if(in_array($u['id'],$arr_root)){
                        $result[$m]['checked']=1;
                    }
                    $top[$k]['child'][]=$result[$m];
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
	public function get_distribution_department(){
		$m = M('DistributionDepartment');
        $result=$m->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无');
        }
    }
    public function getDepartment(){
        $m = M('Department',C('DB_PREFIX_T'), C('db_config'));
        $result=$m->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无');
        }
    }
}